﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace PeisongWeb.admin
{
    public partial class purchaseChargeSeller : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                categoryRootBind();
                categorySubBind();
                rpt_pcslistBind();
            }
        }
        public int adminId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["admin"]))
                {
                    return Convert.ToInt32(Request.QueryString["admin"]);
                }
                return 0;
            }
            set
            {
                adminId = value;
            }
        }
        private void rootCategoryBind()
        {
            DataTable dt = logic.category.listWithParent();
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        private void subCategoryBind()
        {
            //当大类不等于空的时候，就绑定小类
            if (ddl_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
            else
            {
                ddl_CategorySub.Items.Clear();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }
        private void categoryRootBind()
        {
            DataTable dt = logic.category.listWithPurchaseRoot(adminId);
            ddl_adminRootCategory.DataSource = dt;
            ddl_adminRootCategory.DataBind();
            ddl_adminRootCategory.Items.Insert(0,new System.Web.UI.WebControls.ListItem("--请选择--",""));
        }
        private void categorySubBind()
        {
            if (ddl_adminRootCategory.SelectedValue != "")
            {
                DataTable dt = logic.category.listWithPurchaseSub(adminId, Convert.ToInt32(ddl_adminRootCategory.SelectedValue));
                ddl_adminSubCategory.DataSource = dt;
                ddl_adminSubCategory.DataBind();
                ddl_adminSubCategory.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
            else
            {
                ddl_adminSubCategory.Items.Clear();
                ddl_adminSubCategory.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCategoryBind();
        }
        protected void ddl_CategorySub_SelectedIndexChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void ddl_adminRootCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
        }
        protected void ddl_adminSubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            rpt_pcslistBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue))
            {
                where.AppendFormat(" and categoryId={0}", ddl_CategorySub.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txt_CompanyNo.Text))
            {
                where.AppendFormat(" and companyNo='{0}'", txt_CompanyNo.Text);
            }
            DataSet ds = logic.category.listWithCompany(where.ToString(), "categoryId", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
            add_seller.Show();
        }
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbn_select_Click(object sender, EventArgs e)
        {
            rpt_pcslistBind();
        }

        protected void lbn_reset_Click(object sender, EventArgs e)
        {
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                string[] categoryIdAndCompanyId = e.CommandArgument.ToString().Split(',');
                int categoryId = Convert.ToInt32(categoryIdAndCompanyId[0]);
                int companyId = Convert.ToInt32(categoryIdAndCompanyId[1]);
                logic.sysAdmin.chargeSeller(categoryId, adminId, companyId);
                rptBind();
                rpt_pcslistBind();
                Library.Script.ClientMsg("添加成功.");
                
            }
        }
        protected void rpt_pcslist_ItemCommand(object sourse, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int pcsid = Convert.ToInt32(e.CommandArgument.ToString());
                logic.sysAdmin.chargeSellerDel(pcsid);
                categoryRootBind();
                categorySubBind();
                rpt_pcslistBind();
                Library.Script.ClientMsg("删除成功.");
                
            }
        }

        protected void rpt_pcslistBind()
        {
            StringBuilder where = new StringBuilder();
            where.AppendFormat("sysadminId={0}", adminId);
            if (!string.IsNullOrEmpty(ddl_adminSubCategory.SelectedValue))
            {
                where.AppendFormat(" and categoryId={0}", Convert.ToInt32(ddl_adminSubCategory.SelectedValue));
            }
            if (!string.IsNullOrEmpty(txt_sellerNo.Text))
            {
                where.AppendFormat(" and companyNo='{0}'", txt_sellerNo.Text);
            }
            DataSet ds = logic.category.listWithPurchase(where.ToString(), "categoryId", AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            AspNetPager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_pcslist.DataSource = ds.Tables[1];
            rpt_pcslist.DataBind();
        }
        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            rpt_pcslistBind();
        }
        protected void lbn_addseller_Click(object sender, EventArgs e)
        {
            rootCategoryBind();
            subCategoryBind();
            add_seller.Show();
        }
        protected void btnAddSelected_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpt_list.Items.Count; i++)
            {
                RepeaterItem row = rpt_list.Items[i];
                HiddenField hidcid = row.FindControl("hidcid") as HiddenField;
                HiddenField hidcompanyid = row.FindControl("hidcompanyid") as HiddenField;
                CheckBox chkselect = row.FindControl("chkSelected") as CheckBox;
                if (chkselect.Checked)
                {
                    int categoryId = Convert.ToInt32(hidcid.Value);
                    int companyId = Convert.ToInt32(hidcompanyid.Value);
                    logic.sysAdmin.chargeSeller(categoryId, adminId, companyId);
                }
            }
            Library.Script.ClientMsg("添加成功!");
            rptBind();
            rpt_pcslistBind();
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpt_pcslist.Items.Count; i++)
            {
                RepeaterItem row = rpt_pcslist.Items[i];
                HiddenField hidpcsid = row.FindControl("hidpcsid") as HiddenField;
                CheckBox chkselect = row.FindControl("chkChosed") as CheckBox;
                if (chkselect.Checked)
                {
                    logic.sysAdmin.chargeSellerDel(Convert.ToInt32(hidpcsid.Value));
                }
            }
            categoryRootBind();
            categorySubBind();
            rpt_pcslistBind();
        }
    }
}
